(function(window){
	/* start slide show */
	var SlideShow = function(target,width,height,thumbWidth,thumbHeight){
		var self = this;

		this.page = 0;
		this.index = 0;

		this.count = $(target).find('.z-thumbnail-container td a').length;
		this.pictureContainer = $(target).find('.z-large-picture-container');
		this.picture = $(target).find('.z-large-picture-container img.z-large-picture');
		this.thumbnailContainer = $(target).find('.z-thumbnail-container');
		this.thumbnailTable = $(target).find('.z-thumbnail-container table');

		Z.alignImage($(target).find('td img'),thumbWidth,thumbHeight);
		Z.alignImage($(this.picture),width,height);

		$(target).find('.z-next-page-button').click(function(){
			self.nextPage();
		});

		$(target).find('.z-previous-page-button').click(function(){
			self.previousPage();
		});


		$(target).find('.z-next-button').click(function(){
			self.next();
		});

		$(target).find('.z-previous-button').click(function(){
			self.previous();
		});

		$(target).find('.z-thumbnail-container td a').each(function(i){
			$(this).click(function(){
				self.index = i;
				self.go(self.index)
			});
		});

		this.go(this.index);
	};


	SlideShow.prototype = {
		go: function(i){
			var src = $(this.thumbnailTable).find('td a').eq(i).find('img').attr('src'),
			loading = Z.loading($(this.pictureContainer));
			$(this.picture).attr('onabort="this.src=this.src;"');//兼容ie6
			$(this.picture).attr('src',src.replace('_thumb.','.')).load(function(){
				$(loading).fadeOut(300);
			});
		},
		next: function(){
			if(this.index < this.count-1){
				this.go(++this.index);
			}
		},
		previous: function(){
			if(this.index > 0){
				this.go(--this.index);
			}
		},
		nextPage: function(){
			var tableLeft = Math.abs(Z.parseFloat($(this.thumbnailTable).css('margin-left'))),
			tableWidth = $(this.thumbnailTable).width(),
			containerWidth = $(this.thumbnailContainer).width(),
			rightHiddenLength = tableWidth - tableLeft - containerWidth;
			if(tableWidth > containerWidth){
				$(this.thumbnailTable).animate({
					'margin-left':  '-='+(rightHiddenLength>containerWidth?containerWidth:rightHiddenLength)+'px'
				}, 300);
			}
		},
		previousPage : function(){
			var tableLeft = Math.abs(Z.parseFloat($(this.thumbnailTable).css('margin-left'))),
			containerWidth = $(this.thumbnailContainer).width();

			$(this.thumbnailTable).animate({
				'margin-left':  '+='+(tableLeft>containerWidth?containerWidth:tableLeft)+'px'
			}, 300);
		}
	};
	
	window.Z.SlideShow = SlideShow;
	// end slide show
})(window);